package com.hulk.dryad.web.control;

import cn.ms.sequence.IdWorker;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.Limiter;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.constant.CommonConstants;
import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.common.exception.BaseRteException;
import com.hulk.dryad.common.util.SecurityUtils;
import com.hulk.dryad.persistence.entity.SmsSendRecord;
import com.hulk.dryad.persistence.service.SmsSendRecordService;
import com.hulk.dryad.web.api.vo.SMSCheckReqVO;
import com.hulk.dryad.web.api.vo.SMSSendReqVO;
import com.hulk.dryad.web.oauth2.base.service.DryadUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * Created by liqingxue
 * <p/>
 * <p>2020/10/26
 */
@Api(value = " sms", tags = "消息模块")
@ApiSort(4)
@Slf4j
@RestController
@RequestMapping("/sms")
@AllArgsConstructor
public class SMSController {

    private final SmsSendRecordService smsSendRecordService;

    /**
     * <post>消息发送</post>
     *
     * @param req 消息发送
     * @return R
     */
    //@RepeatSubmit
    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 0)
    @ApiOperation(value = "send", notes = "消息发送")
    @PostMapping(value = "/send")
    public void send(@RequestBody @Valid SMSSendReqVO req) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 消息发送,入参:[{}]", workId, req);
        try {
            DryadUser suer = SecurityUtils.getUser();
            // init
            SmsSendRecord record = new SmsSendRecord();
            record.setId(IdWorker.getId());
            record.setMobile(req.getReceiver());
            record.setSmsId(workId);
            record.setSmsType(req.getSmsType());
            record.setSmsCode(RandomStringUtils.randomNumeric(6));
            record.setVerifyFlag("N");
            record.setExpireTime(LocalDateTime.now().plusMinutes(10L));
            record.setReversion(1L);
            record.setCreatedTime(LocalDateTime.now());
            record.setCreatedBy(CommonConstants.DEFUAULT_CREATER);
            // record.insert();
        } catch (BaseRteException ex) {
            log.error("workId:[{}] 消息发送,异常:[{}]", workId, ex);
            throw ex;
        } catch (Exception e) {
            log.error("workId:[{}] 消息发送,异常:[{}]", workId, e);
            throw new BaseRteException(BEC.E_9999);
        }
        log.info("workId:[{}] 消息发送完成!", workId);
    }

    //@Lockd(expire = 200000000000000L )
    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "check", notes = "验证码校验")
    @PostMapping(value = "/check")
    public String check(@RequestBody @Valid SMSCheckReqVO req) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 验证码校验,入参:[{}]", workId, req);
        try {
            DryadUser suer = SecurityUtils.getUser();
            //do check
            // TODO: 2020/12/31
            //Thread.sleep(200000000000000000L);
        } catch (BaseRteException ex) {
            log.error("workId:[{}] 验证码校验,异常:[{}]", workId, ex);
            throw ex;
        } catch (Exception e) {
            log.error("workId:[{}] 验证码校验,异常:[{}]", workId, e);
            throw new BaseRteException(BEC.E_9999);
        }
        log.info("workId:[{}] 验证码校验完成!", workId);
        return "SUCCESS";
    }


    @Idempotent(key = "#smsType+'-'+#smsValue",expireTime = 2000000000)
    @Limiter(capacity = 4, replenish = 2)
    @PrintlnLog
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "check", notes = "验证码校验plus")
    @GetMapping(value = "/checkplus/{smsType}/{smsValue}")
    public String checkPlus(@PathVariable("smsType") String smsType, @PathVariable("smsValue") String smsValue) {
        String workId = IdWorker.getId() + "";
        log.info("workId:[{}] 验证码校验,入参:smsType:[{}],smsValue:[{}]", workId, smsType, smsValue);
        try {
            DryadUser suer = SecurityUtils.getUser();
            //do check
            // TODO: 2020/12/31
            //Thread.sleep(200000000000000000L);
        } catch (BaseRteException ex) {
            log.error("workId:[{}] 验证码校验,异常:[{}]", workId, ex);
            throw ex;
        } catch (Exception e) {
            log.error("workId:[{}] 验证码校验,异常:[{}]", workId, e);
            throw new BaseRteException(BEC.E_9999);
        }
        log.info("workId:[{}] 验证码校验完成!", workId);
        return "SUCCESS";
    }
}
